---
title: "Accessing Rows"
---

This section covers the ways data can be accessed once it has been supplied to the grid.

Each time you pass data to the grid, the grid wraps each data item with a [Row Node](./row-object/) object. For example, if your data has 20 rows, the grid creates 20 Row Node objects, each Row Node wrapping one item of your data.

It is sometimes handy to access these Row Nodes. One example where it is handy is if you want to select a row, you can call `rowNode.setSelected(true)` to select it. This section details the different ways a Row Node can be accessed.

The following methods are provided for accessing the individual Row Nodes. A deeper explanation of these methods, along with examples, is provided further down.

## Accessing Row Node API Methods

{% apiDocumentation source="grid-api/api.json" section="rowNodes" /%}

## Accessing Rows by Row ID

The easiest way to get a Row Node is by its Row ID. The ID is either provided by you using the grid callback `getRowId()`, or generated by the grid using an internal sequence.

```{% frameworkTransform=true %}
const gridOptions = {
    // callback tells the grid to use the 'id' attribute for IDs, IDs should always be strings
    getRowId: params => params.data.id
}
```

To lookup a Row Node use `getRowNode()` on the Grid API as follows:

```{% frameworkTransform=true %}
// get the row node with ID 55
const rowNode = api.getRowNode('55');

// do something with the row, e.g. select it
rowNode.setSelected(true);
```

## Iterating Rows

Sometimes you may want to iterate through all the Row Nodes in the grid. This can be done using the grid's iteration APIs. The iteration APIs go through every Row Node, regardless of whether the Row Node is displayed or not. For example, if grouping and the group is closed, the group's children are not displayed by the grid, however the children are included in the iteration 'for-each' methods.

```{% frameworkTransform=true %}
// iterate through every node in the grid
api.forEachNode((rowNode, index) => {
    console.log('node ' + rowNode.data.athlete + ' is in the grid');
});

// iterate only nodes that pass the filter
api.forEachNodeAfterFilter((rowNode, index) => {
    console.log('node ' + rowNode.data.athlete + ' passes the filter');
});

// iterate only nodes that pass the filter and ordered by the sort order
api.forEachNodeAfterFilterAndSort((rowNode, index) => {
    console.log('node ' + rowNode.data.athlete + ' passes the filter and is in this order');
});

// iterate through every leaf node in the grid
api.forEachLeafNode((rowNode, index) => {
    console.log('node ' + rowNode.data.athlete + ' is not a group!');
});
```

{% note %}
All the methods above work with the Client-Side Row Model, i.e. the default [Row Model](./row-models/).
For all the other row models (i.e. [Viewport](./viewport/),
[Infinite](./infinite-scrolling/) and [Server-Side](./server-side-model/)) the only method
that is supported is `api.forEachNode()` and that will return back Row Nodes that are loaded into
browser memory only (as each of these row models use a data source to lazy load rows).
{% /note %}

## Example Using For-Each Methods

The example below shows the different For-Each API methods as follows:

* **For-Each Node**: Prints out every row in the grid. It ignores filtering and sorting.
* **For-Each Node After Filter**: Prints out every row in the grid, except those filtered out.
* **For-Each Node After Filter and Sort**: Prints out every row in the grid, except those filtered,
  and in the same order they appear on the screen if sorting is applied.
* **For-Each Leaf Node**: Prints out every row in the grid except group rows.

In the example, try applying some sorts and filters, and see how this impacts the different operations in the developer console.

{% gridExampleRunner title="Using For-Each" name="using-for-each"  exampleHeight=590 /%}
